package krt.wid.gslzs.util;

import android.content.Context;
import android.content.SharedPreferences;

import com.google.gson.Gson;

import java.text.SimpleDateFormat;
import java.util.Date;


import krt.wid.gslzs.bean.LoginBean;
import krt.wid.gslzs.bean.MasterXx;


public class ToolSP {

    private Context context;
    private SharedPreferences sp;
    private static ToolSP tsp = new ToolSP();

    public static ToolSP getInstance() {
        return tsp;
    }

    public Context getContext() {
        return context;
    }

    public void setContext(Context context) {
        this.context = context;
    }

    public ToolSP() {

    }


    /**
     * 设置获取json版本日期
     */
    public void setGetver() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        sp.edit().putString("verdate", sdf.format(new Date())).commit();
    }

    public void clearNewver() {
        sp.edit().putString("verdate", "").commit();
    }

    public boolean isNewver() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(new Date()).equals(sp.getString("verdate", ""));
    }

    /**
     * 设置/获取是否第一次启动程序
     *
     * @param ver
     */
    public void setFirstopen(int ver) {
        sp.edit().putInt("firstopen", ver).commit();
    }

    public int getFirstopen() {
        return sp.getInt("firstopen", 0);
    }

    /**
     * 设置/获取用户名
     *
     * @param username 用户名
     */
    public void setUsername(String username) {
        sp.edit().putString("username", username).commit();
    }

    public String getUsername() {
        return sp.getString("username", "");
    }


    /**
     * 存储token
     */
    public void setToken(String token) {
        sp.edit().putString("token", token).commit();
    }

    /**
     * 获取token
     */
    public String getToken() {
        return sp.getString("token", "");
    }

    /**
     * 设置/获取是否保存帐号密码
     *
     * @param ischeck 是否保存
     */
    public void setIsSave(boolean ischeck) {
        sp.edit().putBoolean("isSave", ischeck).commit();
    }

    public boolean getIsSave() {
        return sp.getBoolean("isSave", false);
    }

    /**
     * 设置/获取是否已登录
     *
     * @param auto 是否已登录
     */
    public void setIsLogin(boolean auto) {
        sp.edit().putBoolean("islogin", auto).commit();
    }

    public boolean getIsLogin() {
        return sp.getBoolean("islogin", false);
    }

    /**
     * 检查消息版本号
     *
     * @param ver 最新版本号
     */

    public boolean hasNotice(String ver) {
        boolean hn = sp.getString("noticever", "0").equals(ver);
        sp.edit().putString("noticever", ver).commit();
        return !hn;
    }

    /**
     * 登记信鸽token
     *
     * @param token
     */
    public void updateXGtoken(String token) {
        sp.edit().putBoolean("xgregister", true)
                .putString("xgtoken", token).commit();
    }

    public boolean isRegisterXG() {
        return sp.getBoolean("xgregister", false);
    }

    public String getXGtoken() {
        return sp.getString("xgtoken", "");
    }

    /**
     * 存储图库目录
     *
     * @param path
     */
    public void savePhotoPath(String path) {
        sp.edit().putString("photopath", path).commit();
    }

    public String getPhotoPath() {
        return sp.getString("photopath", "");
    }


    /**
     * 获取日期
     */
    public static String getDate(Context context) {
        SharedPreferences sp = context.getSharedPreferences("weather", Context.MODE_PRIVATE);
        return sp.getString("date", "");
    }

    /**
     * 获取天气
     */
    public static String getWeatherInfo(Context context) {
        SharedPreferences sp = context.getSharedPreferences("weather", Context.MODE_PRIVATE);
        return sp.getString("weatherinfo", "");
    }


    /**
     * 设置/获取用户基本信息
     */

    public void setUserInfo(MasterXx info) {
        Gson gson = new Gson();
        String infostr = gson.toJson(info);
        sp.edit().putString("userinfo", infostr).commit();
    }

    public MasterXx getUserInfo() {
        Gson gson = new Gson();
        return gson.fromJson(sp.getString("userinfo", ""), MasterXx.class);
    }

    /**
     * 存储搜索历史
     */
    public void saveHistoryInfo(Context context, String json) {
        SharedPreferences sp = context.getSharedPreferences("history", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString("his", json);
        editor.apply();
    }

    /**
     * 获取搜索历史
     */
    public String getHistoryInfo(Context context) {
        SharedPreferences sp = context.getSharedPreferences("history", Context.MODE_PRIVATE);
        return sp.getString("his", null);
    }


}
